package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.service.OrderService;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;
import java.util.UUID;

/**
 * 类 名: ValidateCodeController
 * 描 述:
 * 作 者: 蔡畅
 * 创 建：2019年11月26日
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    JedisUtil jedisUtil;

    /**
     * 体检预约
     *
     * @param orderInfoVo
     * @return
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody OrderInfoVo orderInfoVo) {
//        String telephone = (String) map.get("telephone");
//        //从redis中获取缓存的验证码,可以为手机号+RedisConstant.SENDTYPE_ORDER
//        String codeInRedis =jedisPool.getResource().get(telephone+RedisMessageConstant.SENDTYPE_ORDER);
//        String validateCode = (String) map.get("validateCode");
//        //检验手机验证码
//        if (codeInRedis == null || !codeInRedis.equals(validateCode)) {
//            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
//        }
//        Result result = null;
//        //调用体检预约服务
//        try {
//            map.put("orderType", Order.ORDERTYPE_WEIXIN);
//            result = orderService.order(map);
//        } catch (Exception e) {
//            e.printStackTrace();
//            //预约失败
//            return result;
//        }
//        //如果预约成功,发送短信通知
//        if (result.isFlag()) {
//            //预约成功,发送短信通知
//            String orderDate = (String)map.get("orderDate");
//            try{
//                //调用短信发送工具类,发送短信,通知,和时间
//                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,orderDate);
//            } catch (ClientException e) {
//                e.printStackTrace();
//            }
//        }
//        return result;

        String token = orderInfoVo.getToken();
        String validateCode = orderInfoVo.getValidateCode();
        String telephone = orderInfoVo.getTelephone();


        //检验验证码
        String redisCode = jedisUtil.get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if (redisCode == null || !redisCode.equals(validateCode)) {
            return new Result(false, "验证码无效");
        }

        //调用这个接口默认就是微信预约
        orderInfoVo.setOrderType(Order.ORDERTYPE_WEIXIN);
        //调用dubbo服务创建订单
        return orderService.submit(orderInfoVo);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Map map = orderService.findById(id);
        return Result.success("", map);
    }

    @RequestMapping("/getToken")
    public Result getToken() {
        String token = UUID.randomUUID().toString();
        jedisUtil.setex(token, 60 * 60, token);
        return Result.success("", token);

    }
}
